package com.skycode7.Servlet;

import com.skycode7.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/newsDel.jsp")
public class NewsDelServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        String User =(String) req.getSession().getAttribute("User");
        if (User == null || User.equals("")){
            resp.sendRedirect("login.jsp"); //用户没登陆 跳转登陆页面
            return;//页面显示和重定向只能生效一个
        }
        req.setCharacterEncoding("UTF-8");
        String newsId = req.getParameter("NewsID");
        int result = (new NewsDao().delete(Integer.parseInt(newsId)));
        if (result > 0) {
            resp.sendRedirect("news.html");
        }else{
            req.getRequestDispatcher("WEB-INF/error.jsp").forward(req, resp);
        }
    }
}